/*
This software is distributed under the Simplified BSD License:

Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
    	this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
    	this list of conditions and the following disclaimer in the documentation 
    	and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
#ifndef GENERIK_DATABASE_H
#define GENERIK_DATABASE_H

/** \file database.h
This file specifies a generik interface to a database api implementation.
*/

#include <string>
#include <list>
#include "emitter.h"

#include "tracing.h"

namespace generik
{

	template <typename ROW>
	void addROW(int nfields, char** values, void* rowlist)
	{
		((std::list<ROW>*)rowlist)->push_back(ROW(nfields, values));
	}
	
	/*
	template <>
	void addROW<bool>(int nfields, char** values, void* rowlist)
	{
		((std::list<std::string>*)rowlist)->push_back(???);
	}
	*/
	template <>
	void addROW<int>(int nfields, char** values, void* rowlist)
	{
		((std::list<int>*)rowlist)->push_back(int(atoi(values[0])));
	}
	template <>
	void addROW<float>(int nfields, char** values, void* rowlist)
	{
		((std::list<int>*)rowlist)->push_back(int(atof(values[0])));
	}
	template <>
	void addROW<std::string>(int nfields, char** values, void* rowlist)
	{
		((std::list<std::string>*)rowlist)->push_back(std::string(values[0]));
	}
	
	struct database_query_handling
	{
		virtual ~database_query_handling() {}
		
		void execute(const std::string& command)
		{
			runquery(command, 0, 0);
		}
	
		template <typename ROW>
		std::list<ROW> query(const std::string& command)
		{
			std::list<ROW> result;
			runquery(command, &addROW<ROW>, (void*)&result);
			return result;
		}
		
		template <typename ROW>
		bool query(const std::string& command, ROW& row_out)
		{
			std::list<ROW> result = query<ROW>(command);
			if (!result.empty())
			{
				row_out = result.front();
				return true;
			}
			return false;
		}
				
		typedef void(*callback)(int, char**, void*);
	
		virtual void runquery(const std::string& cmd, callback cb, void* data) = 0;
	};
	
	struct database_event_processing
	: generik::emitter<std::string>
	{
		/** Instructs the database instance to listen for the named events
		*/
		virtual void listen(const std::string& table) = 0;
		/** Instructs the database to stop listening for the named events
		*/
		virtual void unlisten(const std::string& table) = 0;
		/** Instructs the database send a named notification event
		*/
		virtual void notify(const std::string& table) = 0;
	};
	
	struct database_module_handler
	{
		/** Loads the specified module and prepares it for execution
		*/
		virtual void load_module(const std::string& path, const std::string& modulename) = 0;
	};
	
	/** This is a generik interface to a database api implementation.
	
	It inherits the interfaces of both database_query_handling and
	database_event_processing, providing the basic methods with which to
	run queries against the database, and to send and receive asynchronous
	signals to and from the database (in this regard it acts as a 
	generik::emitter of strings).
	
	generik::postgres and generik::sqlite3 both derive from this baseclass.
	They serve as wrappers of libpq and sqlite3 respectively, while implementing
	this interface. As such, an application does not need to be aware of
	what kind of database it is talking to.
	*/
	struct database
	: database_query_handling
	, database_event_processing
	, database_module_handler
	{
		database()
		{
			GENERIK_TRACER;
		}
	
		virtual ~database()
		{
			GENERIK_TRACER;
		}
		
		void load_module(const std::string& path, const std::string& modulename)
		{
			GENERIK_TRACER;
			GENERIK_TRACE("NOT YET IMPLEMENTED");
		}
	};
	
	struct dbspi
	: database_query_handling
	{
		dbspi()
		{
			GENERIK_TRACER;
		}
		
		virtual ~dbspi()
		{
			GENERIK_TRACER;
		}
	};
	
	struct stringquery
	{
		std::string value;
		stringquery() {}
		stringquery(int nvalues, char** values)
		{
			int i = 0;
			while (i < nvalues)
			{
				char* v = values[i++];
				if (v)
					value += v;
				if (i < nvalues - 1)
				{
					value += ", ";
				}
			}
		}
	};
	std::string query(database_query_handling* database_in, const std::string& command)
	{
		GENERIK_TRACER;
		GENERIK_TRACE_VALUE((void*)database_in);
		stringquery squery;
		database_in->query<stringquery>(command, squery);
		return squery.value;
	}
	
}

#endif

